package zhihui.mobile.ui.shop.attention;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.SpuInfo;
import zhihui.mobile.config.Config;
import zhihui.mobile.ui.shop.cart.ShopCartAdapter;

/**
 * Created by 张公铭 on 2017/8/15 0015.
 */

public class ShopAttentionAdapter extends BaseAdapter
{
    private Context mContext;
    private LayoutInflater inflater;
    private List<SpuInfo> list = new ArrayList<>();
    private boolean isEdit = false;
    private IOnCheckListener listener;


    public interface IOnCheckListener
    {
        void check(int spuId);

        void unCheck(int spuId);
    }

    public ShopAttentionAdapter(Context context)
    {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    public void setListener(IOnCheckListener listener)
    {
        this.listener = listener;
    }

    public void setList(List<SpuInfo> list)
    {
        this.list = list;
    }

    public void setEdit(boolean edit)
    {
        isEdit = edit;
    }

    @Override
    public int getCount()
    {
        return list.size();
    }

    @Override
    public Object getItem(int position)
    {
        return list.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return list.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        CheckBox cb_select;
        ImageView iv_icon, iv_cart;
        TextView tv_name, tv_price;

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.shop_attention_item, null);
            cb_select = (CheckBox) convertView.findViewById(R.id.cb_shop_attention_item_shop);
            iv_icon = (ImageView) convertView.findViewById(R.id.iv_shop_attention_item_img);
            iv_cart = (ImageView) convertView.findViewById(R.id.iv_shop_attention_item_cart);
            tv_name = (TextView) convertView.findViewById(R.id.tv_shop_attention_item_name);
            tv_price = (TextView) convertView.findViewById(R.id.tv_shop_attention_item_price);


            ShopAttentionAdapter.ViewCache viewCache = new ShopAttentionAdapter.ViewCache();
            viewCache.cb_select = cb_select;
            viewCache.iv_icon = iv_icon;
            viewCache.iv_cart = iv_cart;
            viewCache.tv_name = tv_name;
            viewCache.tv_price = tv_price;

            convertView.setTag(viewCache);
        } else
        {
            ShopAttentionAdapter.ViewCache viewCache = (ShopAttentionAdapter.ViewCache) convertView.getTag();
            cb_select = viewCache.cb_select;
            iv_icon = viewCache.iv_icon;
            iv_cart = viewCache.iv_cart;
            tv_name = viewCache.tv_name;
            tv_price = viewCache.tv_price;
        }

        final SpuInfo spuInfo = list.get(position);
        if (isEdit)
        {
            cb_select.setVisibility(View.VISIBLE);
        } else
        {
            cb_select.setVisibility(View.GONE);
        }
        cb_select.setChecked(false);
        cb_select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    listener.check(spuInfo.getId());
                } else
                {
                    listener.unCheck(spuInfo.getId());
                }
            }
        });
        ImageLoader.getInstance().displayImage(Config.FILE_URL + spuInfo.getSpuimg(), iv_icon);
        iv_cart.setVisibility(View.GONE);
        tv_name.setText(spuInfo.getTitle());
        tv_price.setText(((double) spuInfo.getMinprice() / 100) + "");
        return convertView;
    }

    class ViewCache
    {
        public CheckBox cb_select;
        public ImageView iv_icon, iv_cart;
        public TextView tv_name, tv_price;
    }
}
